//
//  MCChargeInWeighDetailViewController.m
//  ERP
//
//  Created by QA on 15/11/18.
//  Copyright © 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MCChargeInWeighDetailViewController.h"
#import "AutoTextTableViewCell.h"
#import "NSDictionary+DataFormat.h"

@interface MCChargeInWeighDetailViewController ()

@property (nonatomic,strong) NSMutableDictionary *weighInfo;
@property (nonatomic,strong) NSMutableArray *allSetArray;

@end

@implementation MCChargeInWeighDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle: @"原材过磅单明细"];
    }
    return self;
}

- (void)getPassingParameter:(id)object
{
    NSDictionary *dic = object;
    self.weighInfo = [NSMutableDictionary dictionaryWithDictionary:dic];
}

- (void)addweighInfoKey:(id)key toKey:(id)tokey defaults:(id)def format:(id)formatText
                      form:(NSDictionary*)dic toArray:(NSMutableArray*)array
{
    id value = [dic objectFormatForKey:key];
    if (!def) def = @"";
    
    if (!value){
        value = def;
    } else if (formatText) {
        value = [NSString stringWithFormat:formatText,value];
    }
    
    NSDictionary *valueDic = [NSDictionary dictionaryWithObject:value forKey:tokey];
    [array addObject:valueDic];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getweighAllweighs:self.weighInfo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getweighAllweighs:(NSDictionary*)dic
{
    self.allSetArray = [NSMutableArray array];
    
    NSMutableArray *weigh = [NSMutableArray array];
    [self addweighInfoKey:@"过磅单号" toKey:@"过磅单号：" defaults:@"未知" format:nil form:dic toArray:weigh];
    [self addweighInfoKey:@"供应商" toKey:@"原材供应商：" defaults:@"未知" format:nil form:dic toArray:weigh];
    [self addweighInfoKey:@"材料名称" toKey:@"材料名称：" defaults:@"未知" format:nil form:dic toArray:weigh];
//    [self addweighInfoKey:@"单价" toKey:@"材料单价：" defaults:@"未知" format:@"%@元/吨" form:dic toArray:weigh];
//    [self addweighInfoKey:@"金额" toKey:@"材料金额：" defaults:@"未知" format:@"%@元" form:dic toArray:weigh];
    [self addweighInfoKey:@"车号" toKey:@"车       号：" defaults:@"未知" format:nil form:dic toArray:weigh];
    [self addweighInfoKey:@"仓位" toKey:@"仓       位：" defaults:@"未知" format:nil form:dic toArray:weigh];
    [self addweighInfoKey:@"进场称重时间" toKey:@"进场称重时间：" defaults:@"未知" format:nil form:dic toArray:weigh];
    [self addweighInfoKey:@"出场称重时间" toKey:@"出场称重时间：" defaults:@"未知" format:nil form:dic toArray:weigh];
    [self.allSetArray addObject:weigh];
    
    NSMutableArray *weigh01 = [NSMutableArray array];
    [self addweighInfoKey:@"毛重" toKey:@"毛       重：" defaults:@"未知" format:@"%@吨" form:dic toArray:weigh01];
    [self addweighInfoKey:@"皮重" toKey:@"皮       重：" defaults:@"未知" format:@"%@吨" form:dic toArray:weigh01];
    [self addweighInfoKey:@"打印净重" toKey:@"打印净重：" defaults:@"未知" format:@"%@吨" form:dic toArray:weigh01];
    [self addweighInfoKey:@"扣重比例" toKey:@"扣重比例：" defaults:@"未知" format:@"%@%%" form:dic toArray:weigh01];
    [self addweighInfoKey:@"扣重重量" toKey:@"扣重重量：" defaults:@"未知" format:@"%@吨" form:dic toArray:weigh01];
    [self addweighInfoKey:@"供方单据号" toKey:@"供方单据号：" defaults:@"无" format:nil form:dic toArray:weigh01];
    [self addweighInfoKey:@"供方单据量" toKey:@"供方单据量：" defaults:@"未知" format:@"%@吨" form:dic toArray:weigh01];
    [self.allSetArray addObject:weigh01];

}

#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.allSetArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //通用页面
    NSMutableArray *array = [self.allSetArray objectAtIndex:indexPath.section];
    NSString *identifier = [NSString stringWithFormat:@"weighInfoTableViewCell%ld",(long)indexPath.section];
    AutoTextTableViewCell *cell = [[AutoTextTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    [cell setDataArray:array];
    return cell;
}

#pragma mark
#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, 310, 29.5)];
    lable.textColor = ThemeColorBlue;
    //lable.textColor = [UIColor colorWithWhite:0.33 alpha:1.0];
    lable.font = [UIFont boldSystemFontOfSize:15];
    
    if (section  == 0) {
        lable.text = @"过磅单基本信息";
    } else if (section  == 1) {
        lable.text = @"过磅称重信息";
    }
    [view addSubview:lable];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 29.5, 320, 0.5)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
    [view addSubview:lineView];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //通用返回
    NSMutableArray *array = [self.allSetArray objectAtIndex:indexPath.section];
    AutoTextTableViewCell *cell = [[AutoTextTableViewCell alloc] init];
    [cell setDataArray:array];
    return cell.contentView.frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
